package com.cq.hd.app.biz;

import com.cq.hd.app.vo.RedPacketStatisticsVo;
import com.cq.hd.common.enums.biz.ActiveRedPacketStateEnum;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.product.api.TbActiveRedPacketApi;
import com.cq.hd.product.api.TbActiveRedPacketBillApi;
import com.cq.hd.product.api.dto.RedPacketStatisticsDto;
import com.cq.hd.product.api.vo.ActiveRedPacketBillPageVo;
import com.cq.hd.product.api.vo.ActiveRedPacketVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class RedPacketStatisticsBiz {

    @Autowired
    private TbActiveRedPacketApi activeRedPacketApi;

    @Autowired
    private TbActiveRedPacketBillApi activeRedPacketBillApi;

    public Page<RedPacketStatisticsVo> pageRedPacketStatistics(RedPacketStatisticsDto redPacketStatisticsDto) {
        List<RedPacketStatisticsVo> redPacketStatisticsVos = new ArrayList<>();

        // 分页获取红包列表
        Page<ActiveRedPacketVo> activeRedPacketVoPage = activeRedPacketApi.pageValidRedPacket(redPacketStatisticsDto).unpack();
        List<ActiveRedPacketVo> activeRedPacketVos = activeRedPacketVoPage.getList();
        if (!CollectionUtils.isEmpty(activeRedPacketVos)) {
            List<Long> redPacketIds = activeRedPacketVos.stream().map(ActiveRedPacketVo::getId).collect(Collectors.toList());

            Map<Long, List<ActiveRedPacketBillPageVo>> activeRedPacketBillMap = new HashMap<>();
            List<ActiveRedPacketBillPageVo> activeRedPacketBillVos = activeRedPacketBillApi.listActiveRedPacketBillByRedPacketIds(redPacketIds).unpack();
            if (!CollectionUtils.isEmpty(activeRedPacketBillVos)) {
                activeRedPacketBillMap = activeRedPacketBillVos.stream().collect(Collectors.groupingBy(ActiveRedPacketBillPageVo::getRedPacketId));
            }

            for (ActiveRedPacketVo activeRedPacketVo : activeRedPacketVos) {
                Long id = activeRedPacketVo.getId();
                BigDecimal amt = activeRedPacketVo.getAmt();

                // 已领红包总数
                int receiveNum = 0;
                // 已用红包总数
                int useNum = 0;
                // 支出成本=已用红包数量*红包金额
                BigDecimal expendAmt = BigDecimal.ZERO;

                List<ActiveRedPacketBillPageVo> activeRedPacketBillPageVos = activeRedPacketBillMap.get(id);
                if (!CollectionUtils.isEmpty(activeRedPacketBillPageVos)) {
                    receiveNum = activeRedPacketBillPageVos.size();

                    // 统计已用红包总数
                    Map<Integer, List<ActiveRedPacketBillPageVo>> activeRedPacketBillPageVoMap = activeRedPacketBillPageVos.stream()
                            .collect(Collectors.groupingBy(ActiveRedPacketBillPageVo::getState));

                    List<ActiveRedPacketBillPageVo> useActiveRedPacketBillPageVos = activeRedPacketBillPageVoMap.get(ActiveRedPacketStateEnum.YES_UES.getValue());
                    if (!CollectionUtils.isEmpty(useActiveRedPacketBillPageVos)) {
                        useNum += useActiveRedPacketBillPageVos.size();
                    }

                    List<ActiveRedPacketBillPageVo> verifyActiveRedPacketBillPageVos = activeRedPacketBillPageVoMap.get(ActiveRedPacketStateEnum.YES_VERIFY.getValue());
                    if (!CollectionUtils.isEmpty(verifyActiveRedPacketBillPageVos)) {
                        useNum += verifyActiveRedPacketBillPageVos.size();
                    }
                }

                // 计算支出成本=已用红包数量*红包金额
                if (useNum > 0) {
                    expendAmt = amt.multiply(new BigDecimal(useNum));
                }

                // 处理红包标题
                String title = String.format("%s元无门槛", amt.toString());

                RedPacketStatisticsVo redPacketStatisticsVo = new RedPacketStatisticsVo();
                BeanUtils.copyProperties(activeRedPacketVo, redPacketStatisticsVo);
                redPacketStatisticsVo.setTitle(title);
                redPacketStatisticsVo.setReceiveNum(receiveNum);
                redPacketStatisticsVo.setUseNum(useNum);
                redPacketStatisticsVo.setExpendAmt(expendAmt);

                redPacketStatisticsVos.add(redPacketStatisticsVo);
            }
        }

        return Page.newPageWithAllFields(redPacketStatisticsVos, activeRedPacketVoPage.getPageNum(),
                activeRedPacketVoPage.getPageSize(), activeRedPacketVoPage.getTotal(), activeRedPacketVoPage.getPages());
    }

    public BigDecimal getRedPacketTotalCost(Long userId, Long businessId) {
        // 查询红包流水数据
        List<ActiveRedPacketBillPageVo> activeRedPacketBillPageVos = activeRedPacketBillApi.listUsedByBusinessId(userId, businessId).unpack();
        if (CollectionUtils.isEmpty(activeRedPacketBillPageVos)) {
            return BigDecimal.ZERO;
        }

        // 当前累计支出的红包成本总额
        BigDecimal totalExpendAmt = BigDecimal.ZERO;

        Map<Long, List<ActiveRedPacketBillPageVo>> activeRedPacketBillPageMap = activeRedPacketBillPageVos.stream()
                .collect(Collectors.groupingBy(ActiveRedPacketBillPageVo::getRedPacketId));

        for (Long redPacketId : activeRedPacketBillPageMap.keySet()) {
            List<ActiveRedPacketBillPageVo> activeRedPacketBills = activeRedPacketBillPageMap.get(redPacketId);
            ActiveRedPacketBillPageVo activeRedPacketBillPageVo = activeRedPacketBills.get(0);
            BigDecimal amt = activeRedPacketBillPageVo.getAmt();
            int useNum = activeRedPacketBills.size();

            // 支出成本=已用红包数量*红包金额
            BigDecimal expendAmt = amt.multiply(new BigDecimal(useNum));
            totalExpendAmt = totalExpendAmt.add(expendAmt);
        }

        return totalExpendAmt;
    }
}
